package com.libinyn.pdf.utils;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;

/**
 * FreemarkerHelper工具类
 */
public class FreemarkerTemplateUtil {

	private static final String DEFAULT_ENCODING = "UTF-8";


	/**
	 * 获取配置信息
	 * @fileName: FreemarkerTemplateUtil.java
	 * @author: bin.li01
	 * @date: 2017/11/23 18:59
	 * @version: v1.0.0
	 */
	public static Configuration getConfig() throws IOException, TemplateException {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
		cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
		cfg.setDefaultEncoding(DEFAULT_ENCODING);
		// SET PROPERTIES
		Properties prop = new Properties();
		FileInputStream in = new FileInputStream("freemarker.properties");
		prop.load(in);
		in.close();
		cfg.setSettings(prop);
		return cfg;
	}

	/**
	 *
	 * @Name: loadTemplateByFile
	 * @Description:从文件路径加载模板
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年6月6日下午7:17:51
	 * @param dir
	 * @param templateName
	 * @param root
	 * @param targetFile
	 * @return
	 * @throws java.io.IOException
	 * @throws freemarker.template.TemplateException
	 *             String
	 */
	public static String loadTemplateByFile(File dir, String templateName, Map<String, Object> root,
                                            String targetFile) throws IOException, TemplateException {
		Configuration cfg = getConfig();
		String result = null;
		ByteArrayOutputStream os = null;
		Writer out = null;
		FileOutputStream fileOutputStream = null;
		try {
			cfg.setDirectoryForTemplateLoading(dir);
			Template template = cfg.getTemplate(templateName, DEFAULT_ENCODING);

			os = new ByteArrayOutputStream();
			File target = new File(targetFile);
			if (!target.getParentFile().exists()) {
				target.getParentFile().mkdirs();
				target.createNewFile();
			}
			 fileOutputStream = new FileOutputStream(target);
			out = new OutputStreamWriter(fileOutputStream, DEFAULT_ENCODING);
			template.process(root, out);
			out.flush();
			result = os.toString(DEFAULT_ENCODING);
		} finally {
			if (os != null) {
				os.close();
			}
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}
			if (out != null) {
				out.close();
			}

		}
		return result;
	}

	/**
	 *
	 * @Name: loadTemplateByContext
	 * @Description:从classpath路径加载模板
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年6月6日下午7:18:14
	 * @param basePackagePath
	 * @param templateName
	 * @param encoding
	 * @param root
	 * @param targetFile
	 * @return
	 * @throws java.io.IOException
	 * @throws freemarker.template.TemplateException
	 *             String
	 */
	public static String loadTemplateByContext(String basePackagePath, String templateName, String encoding,
                                               Map<String, String> root, String targetFile) throws IOException, TemplateException {
		Configuration cfg = getConfig();
		String result = null;
		ByteArrayOutputStream os = null;
		Writer out = null;
		try {
			cfg.setClassLoaderForTemplateLoading(FreemarkerTemplateUtil.class.getClassLoader(), basePackagePath);
			Template template = cfg.getTemplate(templateName, encoding);
			os = new ByteArrayOutputStream();
			out = new OutputStreamWriter(new FileOutputStream(targetFile), encoding);
			template.process(root, out);
			out.flush();
			result = os.toString(encoding);
		} finally {
			if (os != null) {
				os.close();
			}
			if (out != null) {
				out.close();
			}
		}
		return result;
	}

	/**
	 *
	 * @Name: loadTemplateByStream
	 * @Description:根据节点返回html流直接返回到前台
	 * @author: libin
	 * @Version: V1.00
	 * @param templateName 模板路径和名称
	 * @param encoding 编码
	 * @param data 基础数据
	 * @return
	 * @throws java.io.IOException
	 * @throws freemarker.template.TemplateException
	 *             String
	 */
	public static String loadTemplateByStream(String templateName, String ftlPath,
                                              Class<?> basePath, Object data)
			throws IOException, TemplateException {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
		String result = null;
		StringWriter out = new StringWriter();
		try {
			  // 设置FreeMarker的模版文件位置  
	        cfg.setClassForTemplateLoading(basePath, ftlPath);  
			Template template = cfg.getTemplate(templateName, DEFAULT_ENCODING);
			template.process(data, out);
			result = out.toString();
		} finally {
			if (out != null) {
				out.close();
			}
		}
		return result;
	}
}


